﻿//------------------------------------------------------------------------------
// <pcdd-entitiy>
//     彩票主流业务类库
//     微信支付业务对象
// </pcdd-entitiy>
//------------------------------------------------------------------------------

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Web.UI;

namespace CL.Game.BLL.Tools
{
    /// <summary>
    /// WeChatHelper 业务
    /// </summary>
    public class WeChatHelper
    {
        /// <summary>
        /// 保存页面对象，因为要在类的方法中使用Page的Request对象
        /// </summary>
        private Page page { get; set; }
        /// <summary>
        /// 保存跳转地址
        /// </summary>
        public string redirecturl { get; set; }
        public WeChatHelper(System.Web.UI.Page page)
        {
            this.page = page;
        }
        protected T Get<T>(string url)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "get";
            request.Timeout = 2000;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            StreamReader sr = new StreamReader(response.GetResponseStream(), System.Text.Encoding.UTF8);

            string result = sr.ReadToEnd();
            return JsonConvert.DeserializeObject<T>(result);
        }

        public OAuthUserInfo GetOAuthSnsApiUserInfo()
        {
            OAuthUserInfo userInfo = null;
            string appID = System.Configuration.ConfigurationManager.AppSettings["OAUTHWECHATAPPID"];
            string appSecret = System.Configuration.ConfigurationManager.AppSettings["OAUTH_WECHATAPPSECRET"];
            string code = this.page.Request.QueryString["code"];
            try
            {
                if (!string.IsNullOrEmpty(code))
                {
                    OAuthToken oauthToken = Get<OAuthToken>(string.Format("https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code", appID, appSecret, code));

                    if (oauthToken != null && !string.IsNullOrEmpty(oauthToken.openid) && !string.IsNullOrEmpty(oauthToken.access_token))
                    {
                        userInfo = Get<OAuthUserInfo>(string.Format("https://api.weixin.qq.com/sns/userinfo?access_token={0}&openid={1}&lang=zh_CN", oauthToken.access_token, oauthToken.openid));
                    }
                }
                else
                {
                    string url = string.Format("https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri={1}&response_type=code&scope=snsapi_userinfo&state=123456#wechat_redirect", appID, redirecturl);
                    this.page.Response.Redirect(url, true);
                }
            }
            catch (Exception ex)
            {
                throw;
            }
            return userInfo;
        }
    }

    public class OAuthToken
    {
        public string access_token { get; set; }
        public int expires_in { get; set; }
        public string refresh_token { get; set; }
        public string openid { get; set; }
        public string scope { get; set; }

    }

    public class AccessToken
    {
        public string access_token { get; set; }
        public int expires_in { get; set; }
    }

    public class OAuthUserInfo
    {
        public string openid { get; set; }
        public string nickname { get; set; }
        public int sex { get; set; }
        public string province { get; set; }
        public string city { get; set; }
        public string country { get; set; }
        public string headimgurl { get; set; }
        public List<string> privilege { get; set; }
        public string unionid { get; set; }
    }
}
